<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="1200" okText="发货" @ok="handleSubmit">
    <a-row class="jeecg-modal-wrapper">
      <a-col :span="24" class="jeecg-modal-content">
        <BasicForm @register="registerForm" ref="formRef" name="XlCustomerOrdeForm" >
        </BasicForm>
        <!-- 子表单区域 -->
        <a-tabs v-model:activeKey="activeKey" animated >
          <a-tab-pane tab="商品列表" key="xlCustomerOrderDetail" :forceRender="true">
                <!--引用表格-->
              <BasicTable @register="registerTable" :rowSelection="rowSelection">
                <!--字段回显插槽-->
                <template v-slot:bodyCell="{ column, record, index, text }">
                </template>
              </BasicTable>
          </a-tab-pane>
        </a-tabs>
      </a-col>

    <!-- 评论功能 -->
      <!-- <a-col :span="6" class="jeecg-comment-outer">
        <CommentPanel tableName="xl_customer_orde" :dataId="dataId" otherHeight="220"/>
      </a-col> -->
    </a-row>
    <!-- 弹窗区域 -->
    <div>
    </div>
  </BasicModal>
</template>

<script lang="ts" setup>
import { ref, computed, unref, reactive } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { shipmentFormSchema, orderDetailColumns } from '../XlCustomerOrde.data';
import { shipment, queryOrderDetailNotShipmentById } from '../XlCustomerOrde.api';
import CommentPanel from '/@/components/jeecg/comment/CommentPanel.vue';
import { useUserStore } from '/@/store/modules/user';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage'
import { useMessage } from "/@/hooks/web/useMessage";
const { createMessage } = useMessage();
// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const formDisabled = ref(false);
const activeKey = ref('xlCustomerOrderDetail');

const userStore = useUserStore();


//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, getFieldsValue , validate }] = useForm({
  labelWidth: 150,
  schemas: shipmentFormSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 24 }
});

const dataId = ref('0')
//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await reset();
  setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  formDisabled.value = !data?.showFooter;

  if (!!data.record) {
    dataId.value = data.record?.id;  //主键id

    //表单赋值
    await setFieldsValue({
      ...data.record,
    });

    //重新加载子数据
    reload();
  }
  // 隐藏底部时禁用整个表单
  // setProps({ disabled: !data?.showFooter })  
});

//设置标题
const title = computed(() => ('订单发货'));

//注册table数据
const { prefixCls, tableContext, } = useListPage({
  tableProps: {
    title: '商品列表',
    api: queryOrderDetailNotShipmentById,
    columns: orderDetailColumns,
    showActionColumn: false,
    canResize: false,
    showSummary: true,
    beforeFetch: (params) => {
      params.id = dataId.value
      return Object.assign(params);
    },
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext


async function reset() {
  await resetFields();
  activeKey.value = 'xlCustomerOrderDetail';
}

//表单提交事件
async function handleSubmit() {
  try {
    var values = await validate();
    if(!selectedRowKeys.value || selectedRowKeys.value.length < 1
    ){
      createMessage.warning('请选择发货的商品明细');
      return;
    }
    values = {
      ...values,
      xlCustomerOrderDetailList: selectedRows.value,
    }


    setModalProps({ confirmLoading: true });
    //提交表单
    await shipment(values);
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}

</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
:deep(.ant-input-number) {
  width: 100%;
}

:deep(.ant-calendar-picker) {
  width: 100%;
}

/*update-begin-author:taoyan date:2022-7-27 for:modal评论区域样式*/
.jeecg-comment-outer {
  border-left: 1px solid #f0f0f0;

  .ant-tabs-nav-wrap {
    /*  text-align: center;*/
  }
}

.jeecg-modal-content {
  >.scroll-container {
    //update-begin---author:wangshuai---date:2023-12-05---for:【QQYUN-7297】表单讨论弹窗放大按钮时只显示一部分---
    padding: 6px;
    //update-end---author:wangshuai---date:2023-12-05---for:【QQYUN-7297】表单讨论弹窗放大按钮时只显示一部分---
  }
}

/*update-end-author:taoyan date:2022-7-27 for:modal评论区域样式*/

// wrapper设为100%，兼容之前写过的弹窗自定义样式
.jeecg-modal-wrapper,
.jeecg-modal-content {
  height: 100%;
}

html[data-theme='dark'] {
  .jeecg-comment-outer {
    border-left: 1px solid rgba(253, 253, 253, 0.12);
  }
}
</style>